package site.luojie.netty.rpc.server;

import com.alibaba.fastjson.JSON;
import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.CreateMode;
import site.luojie.netty.rpc.common.Server;
import site.luojie.netty.rpc.server.service.UserServiceImpl;

import java.time.LocalDateTime;

/**
 * 服务提供者启动主程序类
 *
 * @author jie.luo
 * @since 2020-07-06 23:32
 **/
public class NettyRpcServerBootstrap1 {
    public static void main(String[] args) throws Exception {

        String host = "127.0.0.1";
        int port = 9999;

        System.out.println("开始创建连接： " + LocalDateTime.now());
        ZkClient zkClient = new ZkClient("test_server:2181");
        System.out.println("完成创建连接： " + LocalDateTime.now());

        String string = JSON.toJSONString(new Server(host, port));

        System.out.println("创建节点： " + LocalDateTime.now());

        String root = "/servers";

        boolean exists = zkClient.exists(root);
        if (!exists){
            zkClient.createPersistent("/servers");
        }

        zkClient.createEphemeralSequential("/servers/server", string);
        System.out.println("创建节点完成" + LocalDateTime.now());

        UserServiceImpl.startServer(host, port);
    }
}
